package com.vegetable.modules.controller.finance;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.finance.Transactions;
import com.vegetable.modules.entity.finance.po.TransactionsQueryPO;
import com.vegetable.modules.entity.finance.po.TransactionsUpdatePO;
import com.vegetable.modules.entity.finance.vo.TradingFlowStatisticsVO;
import com.vegetable.modules.service.finance.ITransactionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * tradingFlow 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-04-15
 */
@Slf4j
@RestController
@RequestMapping("/tradingFlow")
@Api(value = "银行交易模块", tags = "银行交易模块")
public class TransactionsController extends AbstractController {
    @Resource
    private ITransactionsService iTransactionsService;

    @SysLog(value = "批量上传交易流水数据")
    @PostMapping("/upload")
    @ApiOperation(value = "根据id删除班组付款", notes = "根据id删除班组付款")
    public R updateTradeFlow(@RequestParam(value = "file", required = true) MultipartFile file,
                        @RequestParam(value = "accountId", required = true) Long accountId) throws Exception {
            return iTransactionsService.saveBatch(file, accountId);
    }

    @SysLog(value = "查询交易流水")
    @PostMapping("/list")
    @ApiOperation(value = "查询交易流水", notes = "查询交易流水")
    public R list(@RequestBody TransactionsQueryPO po) throws Exception {
        log.info("查询交易流水 {}", JSONObject.toJSONString(po));
        IPage<Transactions> result = iTransactionsService.queryList(po);
        return R.data(result);
    }

    @SysLog(value = "统计交易流水")
    @PostMapping("/statisticsFlow")
    @ApiOperation(value = "查询交易流水", notes = "查询交易流水")
    public R statisticsFlow(@RequestBody TransactionsQueryPO po) throws Exception {
        List<TradingFlowStatisticsVO> result = iTransactionsService.statisticsFlow(po);
        return R.data(result);
    }

    @SysLog(value = "修改流水归属")
    @PostMapping("/updateFlowBelong")
    @ApiOperation(value = "updateFlowBelong", notes = "updateFlowBelong")
    public R updateFlowBelong(@RequestBody TransactionsUpdatePO po) throws Exception {
        log.info("修改流水归属 {}", JSONObject.toJSONString(po));
        Integer result = iTransactionsService.updateFlowBelong(po);
        return R.success("成功更新" + result + "条流水");
    }

    @SysLog(value = "根据adId分页查询代办、农户的所有交易流水")
    @GetMapping("/listByAdId")
    @ApiOperation(value = "根据adId分页查询代办、农户的所有交易流水", notes = "根据adId分页查询代办、农户的所有交易流水")
    public R listByAdId(
            @ApiParam(value = "代办、农户ID", required = true) @RequestParam("adId") Long adId,
            @ApiParam(value = "页码", required = true) @RequestParam("page") Integer page,
            @ApiParam(value = "每页条数", required = true) @RequestParam("limit") Integer limit) throws Exception {
        IPage<Transactions> result = iTransactionsService.queryListByAdId(adId, page, limit);
        return R.data(result);
    }

}